package com.scenic.scenicservice.controller;

import com.github.pagehelper.PageInfo;
import com.scenic.common.bean.RespBean;
import com.scenic.common.bean.ScenicSpot;
import com.scenic.common.exception.ScenicSpotException;
import com.scenic.common.service.ScenicSpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/scenicSpot")
public class ScenicSpotController {
    @Autowired
    private ScenicSpotService scenicSpotServier;

    //添加
    @PostMapping
    public RespBean insert(@RequestBody ScenicSpot scenicSpot) throws ScenicSpotException {
        //scenicSpot.setScore(5f);
        scenicSpotServier.insert(scenicSpot);

        return RespBean.ok("添加成功");
    }
    //根据id删除
    @DeleteMapping(("/{id}"))
    public RespBean delete(@PathVariable("id") Long id) throws ScenicSpotException {
        scenicSpotServier.delete(id);

        return RespBean.ok("删除成功");
    }
    //根据id修改
    @PutMapping
    public RespBean update(@RequestBody ScenicSpot scenicSpot) throws ScenicSpotException {
        scenicSpotServier.update(scenicSpot);

        return RespBean.ok("修改成功");
    }
    //条件分页搜索
    @GetMapping
    public RespBean selectByPage (ScenicSpot condition, Integer pageNum, Integer pageSize){
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;

        PageInfo<ScenicSpot> pageInfo = scenicSpotServier.selectByCondition(condition, pageNum, pageSize);

        return RespBean.ok("",pageInfo);
    }
    @GetMapping("/search")
    public RespBean selectByPage1 (ScenicSpot condition, Integer pageNum, Integer pageSize){
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;

        PageInfo<ScenicSpot> pageInfo = scenicSpotServier.selectByCondition1(condition, pageNum, pageSize);

        return RespBean.ok("",pageInfo);
    }
    // 查询排名前四的旅游景点
    @GetMapping("/rank")
    public RespBean selectRank(){
        ScenicSpot condition = new ScenicSpot();
        condition.setStatus(1);
        condition.setIsRecommended(1);
        condition.setStar(5f);
        Object data = scenicSpotServier
                .selectByCondition(condition, 1, 5);
        data = ((PageInfo<ScenicSpot>) data).getList()
                .stream()
                .limit(4)
                .collect(java.util.stream.Collectors.toList());
        return RespBean.ok("", data);
    }
    //根据id查询
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") Long id) throws ScenicSpotException {
        return RespBean.ok("",scenicSpotServier.selectById(id));
    }

    //热门商品前5
    @GetMapping("/scenicSpotTop5")
    public RespBean scenicSpotTop5(){

        List<Map<String, Object>> list = scenicSpotServier.scenicSpotSaleTop5();

        return RespBean.ok("",list);
    }
}
